using UnityEngine;
using System.Collections;

[System.Serializable]
public class PlaySoundOnWalk_Simple : MonoBehaviour {
	
	public AudioSource source;
	
	public AudioClip soundLeftFootForward;
	public AudioClip soundRightFootForward;
	
	public AudioClip soundLeftFootBackwards;
	public AudioClip soundRightFootBackwards;
	
	public AudioClip soundLeftFootSideStep;
	public AudioClip soundRightFootSideStep;
	
	private static int FORWARD = 0;
	private static int BACKWARD = 1;
	private static int LEFT = 2;
	private static int RIGHT = 3;
	float time = 0f;
	public float timeBetweenSteps = 0.7f;
	int sound = 0;

	// Use this for initialization
	void Start () {
		time = Time.time;
	}
	
	// Update is called once per frame
	void Update () {
		if (Input.GetKey("w")) {
			playSound(FORWARD);
		} else if (Input.GetKey("s")) {
			playSound(BACKWARD);	
		} else if (Input.GetKey("a")) {
			playSound(LEFT);
		} else if (Input.GetKey("d")) {
			playSound(RIGHT);
		}
	}
	
	public void playSound(int direction) {
		if (!source.isPlaying && Time.time-time > timeBetweenSteps) {
			if (direction == FORWARD) {
				if (sound == 0) {
					source.PlayOneShot(soundLeftFootForward);
					sound++;
				}
				else {
					source.PlayOneShot(soundRightFootForward);
					sound = 0;
				}
			} else if (direction == BACKWARD) {
				if (sound == 0) {
					source.PlayOneShot(soundLeftFootBackwards);
					sound++;
				}
				else {
					source.PlayOneShot(soundRightFootBackwards);
					sound = 0;
				}
			} else if (direction == LEFT || direction == RIGHT) {
				if (sound == 0) {
					source.PlayOneShot(soundLeftFootSideStep);
					sound++;
				}
				else {
					source.PlayOneShot(soundRightFootSideStep);
					sound = 0;
				}
			}
			time = Time.time;
		}
	}
}
